#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Data;
using System.Linq;
using Flynn.Core.Activities;
using Flynn.Core.ActivityResults;
using Flynn.Core.TaskResults;
using Flynn.Core.Tasks;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Tasks
{
    [TestFixture]
    public class CheckDatabaseTaskFixture
    {
        private MockRepository _mocks;

        private IGetCurrentVersionActivity _getCurrentVersionActivity;
        private ICheckDatabaseTask _checkDatabaseTask;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _getCurrentVersionActivity = _mocks.CreateMock<IGetCurrentVersionActivity>();
            _checkDatabaseTask = new CheckDatabaseTask(_getCurrentVersionActivity);
        }

        [Test]
        [ExpectedException(typeof (ArgumentNullException))]
        public void GetCurrentVersionActivity_Must_Not_Be_Null()
        {
            new CheckDatabaseTask(null);
        }

        [Test]
        [ExpectedException(typeof (ArgumentNullException))]
        public void When_Checking_Database_Project_Must_Not_Be_Null()
        {
            _checkDatabaseTask.CheckDatabase(null, _mocks.Stub<IDbConnection>());
        }

        [Test]
        [ExpectedException(typeof (ArgumentNullException))]
        public void When_Checking_Database_Connection_Must_Not_Be_Null()
        {
            _checkDatabaseTask.CheckDatabase(new Project(), null);
        }

        [Test]
        public void Can_Check_Database()
        {
            Project project = new Project();
            project.AddUpdate(new Update("1"));
            project.AddUpdate(new Update("2"));

            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                GettingCurrentVersionResult gettingCurrentVersionResult = GettingCurrentVersionResult.Success("1");

                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(gettingCurrentVersionResult);
            }

            using (_mocks.Playback())
            {
                CheckingDatabaseResult result = _checkDatabaseTask.CheckDatabase(project, connection);

                Assert.IsFalse(result.Failed);
                Assert.AreEqual("1", result.CurrentVersion);
            }
        }

        [Test]
        public void Checking_Database_Fails_If_Exception_Is_Thrown()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                CheckingDatabaseResult result = _checkDatabaseTask.CheckDatabase(project, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Checking_Database_Fails_If_Getting_Current_Version_Fails()
        {
            Project project = new Project();
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                GettingCurrentVersionResult gettingCurrentVersionResult = GettingCurrentVersionResult.Failure("Failure message");
                Expect.Call(_getCurrentVersionActivity.GetCurrentVersion(project, connection)).Return(gettingCurrentVersionResult);
            }

            using (_mocks.Playback())
            {
                CheckingDatabaseResult result = _checkDatabaseTask.CheckDatabase(project, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}